const App = {
    data() {
        return {
            photo,
            ACCESSTOKEN: '',
            total: 0,
            loading: false,
            formData: {},
            tableData: [],
            Main:{}
        }
    },
    beforeCreate() {
        const that = this;
        function setRem() {
            const designWidth = 750; // 设计稿宽度（rpx）
            const maxWidth = 540; // 可选：限制最大宽度（如手机端常见值）
            const screenWidth = Math.min(window.innerWidth, maxWidth);
            const scale = screenWidth / designWidth;
            document.documentElement.style.fontSize = scale + 'px';
        }
        setRem();//1rem = 0.5px 对标小程序在 375px 宽的屏幕上，1rpx = 0.5px（因为 750rpx / 375px = 2）
        window.addEventListener('resize', setRem);
    },
    mounted() {
        let URL = window.location.href;
        this.ACCESSTOKEN = getQueryObject(URL, 'ACCESSTOKEN');
        let id = getQueryObject(URL, 'id');
        this.getList(id);
        this.getData(id);
    },
    computed: {
        electronic_voting_type() {
            const { Start_DateTime, End_DateTime } = this.formData;
            let str = '';
            if (Start_DateTime && End_DateTime) {
                const li_Start_DateTime = +new Date(Start_DateTime),
                    li_now_DateTime = +new Date(),
                    li_End_DateTime = +new Date(End_DateTime)
                if (li_now_DateTime < li_Start_DateTime) {
                    str = '活动开始';
                } else if (li_now_DateTime >= li_Start_DateTime && li_now_DateTime <= li_End_DateTime) {
                    str = '活动进行中';
                } else if (li_now_DateTime > li_End_DateTime) {
                    str = '活动结束';
                }
            }
            return str;
        }
    },
    methods: {
        getList(id) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 't_electronic_voting_mini1',
                    Condition: window.btoa(unescape(encodeURIComponent(id)))
                },
                success: (res) => {
                    if (res.length > 0) {
                        this.formData = res[0];
                    }
                },
                complete: (XMLHttpRequest) => {
                    if (XMLHttpRequest.status == 302) {
                        wx.miniProgram.navigateTo({
                            url: miniProgram,
                        })
                    }
                },
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/customForm/statisticalForm?Data=${id}`,
                method: "get",
                success: ({ Main, Detail }) => {
                    this.Main = Main;
                    let tableData = sortupgrade(Detail,'Amount');
                    tableData.reverse();
                    this.tableData = tableData;
                },
                complete: (XMLHttpRequest) => {
                    if (XMLHttpRequest.status == 302) {
                        wx.miniProgram.navigateTo({
                            url: miniProgram,
                        })
                    }
                },
            });
        },
        getData(id){
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'electronic_voting',
                    Condition: `Electronic_Voting_SerialNumber='${id}'`
                },
                success: (electronic_voting) => {
                    if (electronic_voting.length == 1) {
                        let Browse_Count = parseInt(electronic_voting[0].Browse_Count);
                        Browse_Count++;
                        electronic_voting[0].Browse_Count = Browse_Count;
                        let lJSONObject_DataSet = {
                            Modify: { electronic_voting }
                        }
                        this.submitSave(lJSONObject_DataSet);
                    }
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            });
        },
        handleClick(item) {//前往添加调查问卷
            if (+new Date() < +new Date(this.formData.End_DateTime) && +new Date() > +new Date(this.formData.Start_DateTime)) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/requestForward/requestForward?RequestURL=${encodeURIComponent(`/micro/smart/mohe/resident/getVoteCountByElectronicVotingSerialNumberAndPhone?phone=&electronicVotingSerialNumber=${this.formData.Electronic_Voting_SerialNumber}`)}`,
                    type: "GET",
                    success: (res) => {
                        if (res.data.cnt == 0) {
                            window.location.href = h5 + item.Table_Name + '.html?ACCESSTOKEN=' + this.ACCESSTOKEN;
                        } else {
                            this.$message({
                                message: '已参加投票，无法重复参加',
                                type: 'warning'
                            });
                        }
                    },
                    complete: (XMLHttpRequest) => {
                        if (XMLHttpRequest.status == 302) {
                            wx.miniProgram.navigateTo({
                                url: miniProgram,
                            })
                        }
                    },
                    dataType: "json",
                });
            }
        },
        submitSave(Data, response) {
            const websocket = new WebSocketClient();
            websocket.init({
                url: ws + '/generalSave/' + getCookie('token'),
                onopen: () => {
                    let lJSONObject_DataSet = {
                        Data, AccessToken: getCookie('token')
                    }
                    websocket.send(JSON.stringify(lJSONObject_DataSet));
                },
                onmessage: (data) => {
                    try {
                        const res = JSON.parse(data);
                        websocket.close();
                        if (res.REDIRECT && res.REDIRECT == 'REDIRECT') {//重定向
                            wx.miniProgram.navigateTo({
                                url: miniProgram,
                            })
                        } else {
                            response ? response(res) : null;
                        }
                    } catch (error) { }
                },
            })
        }
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");